﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using TappUp_Refresh;
using TappUpsRefresh.Extras;
using ImmunityBuster;
using ImmunityBuster._2D;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Audio;
using Rushabh.Logic.ArtificialIntelligence;
using Rushabh.Logic.Missiles;

namespace ImmunityBuster_wp7_2
{
    public enum RunStates
    {
        running = 1, symptoms = 2
    }

    public enum Levels
    {
        Level1 = 10,
        Level2 = 20,
        Level3 = 30,
        Bonus = 100,
    };

    public class RunningGame
    {
        ContentManager content;
        public Sprite virus = new Sprite();
        ParallaxingBackground pb1 = new ParallaxingBackground();
        ParallaxingBackground pb2 = new ParallaxingBackground();
        Sprite Map = new Sprite();
        RBC2D rbc;
        Levels currentLevel = Levels.Level1;
        String Message { get; set; }
        Sprite BottomPanel;
        ParallaxingBackground bg = new ParallaxingBackground();
        RunStates thisState = new RunStates();
        TimeSpan currentTime = DateTime.Now.TimeOfDay;    
        Sprite symtomps = new Sprite();
        Enemy2D enemy;
        Sprite HealthBar = new Sprite();
        Sprite SpiritBar = new Sprite();
        SoundEffect BackGround;
        public SoundEffectInstance dSe;
        Rushabh.Logic.ArtificialIntelligence.AIEngine _aiEngine = new Rushabh.Logic.ArtificialIntelligence.AIEngine();
        Power power1 = new Power();
        Power power2 = new Power();
        Power power3 = new Power();
        List<Power> powers = new List<Power>();       
        Sprite CrossHair = new Sprite();
        
        public int Vspirit
        {
            get
            {
                if (SpiritBar.Bounds.Width > 100)
                    SpiritBar.Bounds.Width = 100;
                return SpiritBar.Bounds.Width;
            }
            set
            {
                if (SpiritBar.Bounds.Width > 100)
                    SpiritBar.Bounds.Width = 100;
                SpiritBar.Bounds.Width = value;
            }
        }
        public int Vhealth
        {
            get
            {
                return HealthBar.Bounds.Width * 100;
            }
            
            set
            {
                HealthBar.Bounds.Width = value;              
            }
        }
        public RunningGame(ContentManager content)
        {
            this.content = content;
            Map.Velocity = new Vector2(-1.1f, 0);
            rbc = new RBC2D(content);
            virus.Position = new Vector2(0, Randomizer.Random(130, 330));
            rbc = new RBC2D(content);
            BottomPanel = new Sprite();
            enemy = new Enemy2D(content);
            _aiEngine.setContentManager(content);
            Profile prof = Profile.Retrieve();            
            //currentScore.Name = prof.Name;
            //currentScore.photoPath = prof.ImageName;
            //currentScore.Country = prof.Country;
            //currentScore.Difficulty = prof.Difficulty;
            //currentScore.score = 0;
        }

        public void LoadContent()
        {
            virus.Texture = content.Load<Texture2D>("vfx\\VirusMain");
            virus.animation.Initialize(virus.Texture, virus.Position, 150, 300, 8, 100, Color.White, 0.8f, true);
            virus.Velocity = new Vector2(+1f, +1f);
            pb1.Initialize(content, "NEW\\bglayer1", 800, -1);
            pb2.Initialize(content, "NEW\\bglayer2", 800, -3);
            //BottomPanel.Texture = content.Load<Texture2D>("panels\\bottompanel");
            //BottomPanel.Bounds = new Microsoft.Xna.Framework.Rectangle(0, 400, 800, 80);
            Map.Texture = content.Load<Texture2D>("NEW\\background");

            enemy.InitEnemies(Levels.Level1);
            HealthBar.Texture = content.Load<Texture2D>("panels\\Health");
            HealthBar.Bounds = new Microsoft.Xna.Framework.Rectangle(0, 400, 200, 80);
            SpiritBar.Texture = content.Load<Texture2D>("panels\\spirit2");
            SpiritBar.Bounds = new Microsoft.Xna.Framework.Rectangle(600, 400, 200, 80);
            BackGround = content.Load<SoundEffect>("Sounds\\Leviathan");
            power1.powerTile.Texture = content.Load<Texture2D>("NEW/pow1");
            power2.powerTile.Texture = content.Load<Texture2D>("New/pow2");
            power3.powerTile.Texture = content.Load<Texture2D>("New/Pow3");
            power1.powerTile.Bounds = new Microsoft.Xna.Framework.Rectangle(275 + 25, 400, 75, 80);
            power2.powerTile.Bounds = new Microsoft.Xna.Framework.Rectangle(350 + 25, 400, 75, 80);
            power3.powerTile.Bounds = new Microsoft.Xna.Framework.Rectangle(425 + 25, 400, 75, 80);
            power1.ActivePosition.Texture = content.Load<Texture2D>("vfx/animationelectric");
            power2.ActivePosition.Texture = content.Load<Texture2D>("vfx/doppler effect");
            powers.Add(power1);
            powers.Add(power2);
            powers.Add(power3);
            CrossHair.Texture = content.Load<Texture2D>("crossHair");
            dSe = (SoundEffectInstance)BackGround.CreateInstance();
            dSe.Play();
        }

        public States Update(GameTimerEventArgs gameTime, GestureSample gs)
        {
            if (dSe.State != SoundState.Playing)
                dSe.Play();
            
            #region Remain Inside the map!
            if (virus.Position.X > 700)
                virus.Position.X = 700;
            if (virus.Position.X < 50)
                virus.Position.X = 50;
            if (virus.Position.Y < 200)
                virus.Position.Y = 200;
            if (virus.Position.Y > 400)
                virus.Position.Y = 400;
            #endregion

            #region Kill the Virus
            if (Vhealth < 1)
            {
                //return States.Lost;            
            }
            #endregion

            #region Regenerate
            if (HealthBar.Bounds.Width <= 100)
                HealthBar.Bounds.Width += (int)0.1f;
            #endregion            

            pb1.Update();
            pb2.Update();
            enemy.Update(gameTime);
            rbc.Update(gameTime, virus.Bounds);
            virus.Update(gameTime);

            #region Damage
            try
            {
                foreach (ImmunityBuster._2D.Enemy2D.IEnemy enemies in enemy.gameSprites)
                {
                    Microsoft.Xna.Framework.Rectangle tempRec = new Microsoft.Xna.Framework.Rectangle((int)virus.Position.X, (int)virus.Position.Y, virus.animation.FrameWidth, virus.animation.FrameHeight);
                    Microsoft.Xna.Framework.Rectangle tempRec2 = enemies.GetRectangle();
                   if(tempRec.Intersects(tempRec2))
                    {
                        Vhealth -= (int)1f;
                        enemies.InfictDamage(1);
                    }
                }
            }
            catch (Exception _e)
            { }

            foreach (Missile missile in AIEngine.missiles)
            {
                if (missile._destRect.Intersects(virus.Bounds))
                {
                    Vhealth -= 30;
                }
            }
            #endregion

            #region Bar Control

            #endregion

            #region AIEngine
            enemy.gameSprites = _aiEngine.MaintainDistance(enemy.gameSprites, ref virus.Position);
            #endregion

            #region Absorb RBC
            foreach (RBCBase rbcz in rbc.EnemyList)
            {
                if (virus.Bounds.Intersects(rbcz.EnemyAnimation.destinationRect))
                {
                    rbcz.EnemyAnimation.Active = false;
                    if (Vhealth < 90)
                        Vhealth += 10;
                    else
                        if (Vhealth < 100 && Vhealth >= 90)
                        {
                            Vhealth = 100;
                        }


                    if (Vspirit < 95)
                        Vspirit += 5;
                    else
                        Vspirit = 100;
                }
            }
            #endregion

            #region BulletFlow
            _aiEngine.Shoot(enemy.gameSprites, ref virus.Position);
            _aiEngine.Update(gameTime);
            #endregion


            if (gs.GestureType == GestureType.Tap)
            {
                Microsoft.Xna.Framework.Point p = new Microsoft.Xna.Framework.Point();
                p.X = (int)gs.Position.X;
                p.Y = (int)gs.Position.Y;

                CrossHair.Bounds.X = (int)gs.Position.X;
                CrossHair.Bounds.Y = (int)gs.Position.Y;

                foreach (Power power in powers)
                {
                    if (power.powerTile.Bounds.Contains(p))
                    {
                        Power.Active = !Power.Active;
                    }
                }
            }

            if(gs.GestureType == GestureType.Tap)
            {
                
                Microsoft.Xna.Framework.Point p = new Microsoft.Xna.Framework.Point();
                p.X = (int)gs.Position.X;
                p.Y = (int)gs.Position.Y;

                CrossHair.Bounds.X = (int)gs.Position.X;
                CrossHair.Bounds.Y = (int)gs.Position.Y;

                if (Power.Active)
                {
                    foreach (Enemy2D.IEnemy enemyz in enemy.gameSprites)
                    {
                        if (enemyz.GetRectangle().Contains(p))
                        {
                            if (Vspirit >= 20)
                            {
                                enemyz.InfictDamage(40);
                                Vspirit -= 20;
                                Power.Active = !Power.Active;
                            }
                        }
                    }
                }
            }

            return States.playing;
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(Map.Texture, Map.Position, Color.White);
            pb1.Draw(spriteBatch);
            pb2.Draw(spriteBatch);
            rbc.Draw(spriteBatch);
            enemy.Draw(spriteBatch);
            virus.animation.Draw(spriteBatch, Color.White);
            _aiEngine.Draw(spriteBatch);
            //spriteBatch.Draw(BottomPanel.Texture, BottomPanel.Bounds, Color.White);
            spriteBatch.Draw(HealthBar.Texture, HealthBar.Bounds, Color.White);
            spriteBatch.Draw(SpiritBar.Texture, SpiritBar.Bounds, Color.White);
            spriteBatch.Draw(CrossHair.Texture, CrossHair.Bounds, Color.White);
            foreach (Power power in powers)
            {
                spriteBatch.Draw(power.powerTile.Texture, power.powerTile.Bounds, Color.White);               
            }
        }

        internal void InitEnemies(int number)
        { 
        
        }
    }
}



public class Randomizer
{
    public static int Random()
    {
        return new Random().Next();    
    }

    public static int Random(int min, int max)
    {
        return new Random().Next(min, max);
    }

}


public class Power
{
    public Sprite powerTile = new Sprite();
    public Sprite ActivePosition = new Sprite();
    public static bool Active { get; set; }
    public bool activeOrNot;
}
